package me.denger.database2Word.config;

import java.io.*;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.Properties;

/**
 * 配置操作
 */
public abstract class ConfigService {

    /**
     * 配置文件名称
     */
    public static final String CONFIG_FILE_NAME = "database2Word.properties";

    /**
     * 获取数据库配置
     *
     * @return 配置
     */
    public static ConfigBean read() {
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = ConfigService.class.getClassLoader().getResourceAsStream(CONFIG_FILE_NAME);
        return read(in);
    }

    /**
     * 获取数据库配置
     *
     * @param in 输入流
     * @return 配置
     */
    public static ConfigBean read(InputStream in) {
        if (null == in) {
            return new ConfigBean();
        }
        Properties properties = new Properties();
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
            return new ConfigBean();
        }

        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String userName = properties.getProperty("userName");
        String password = properties.getProperty("password");
        String fileName = properties.getProperty("fileName");

        ConfigBean configBean = new ConfigBean(driver, url, userName, password, fileName);
        return configBean;
    }

    /**
     * 获取数据库配置(外部)
     *
     * @param filePath 文件路径
     * @return 配置
     */
    public static ConfigBean readOut(String filePath) {
        File file = new File(filePath);
        if (file.isDirectory() || !file.exists()) {
            return new ConfigBean();
        }
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return new ConfigBean();
        }
        return read(fileInputStream);
    }

    /**
     * 获取数据库配置(外部)
     *
     * @return 配置
     */
    public static ConfigBean readOut() {
        String filePath = getJarPath() + File.separatorChar + CONFIG_FILE_NAME;
        return readOut(filePath);
    }

    /**
     * 获取该jar的完整文件
     *
     * @return 文件路径
     */
    public static String getJarFileNamePath() {
        //获取当前jar的路径
        URL location = ConfigService.class.getProtectionDomain().getCodeSource().getLocation();
        // 根据操作系统做处理
        String filePath = location.getFile();
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            //windows 移除第一个字符
            filePath = filePath.substring(1);
        }
        return filePath;
    }

    /**
     * 获取该jar所在的文件路径
     *
     * @return 文件路径
     */
    public static String getJarPath() {
        File jarPathFile = new File(getJarFileNamePath());
        return jarPathFile.getParent();
    }

    /**
     * 保存到配置文件
     *
     * @param configBean 配置
     */
    public static void writeOut(ConfigBean configBean) {
        String filePath = getJarPath() + File.separatorChar + CONFIG_FILE_NAME;
        File file = new File(filePath);
        // if (file.exists()) {
        //     file.delete();
        //     file = new File(filePath);
        // }

        Properties properties = new Properties();
        if (configBean.getDriver() != null) {
            properties.setProperty("driver", configBean.getDriver());
        }
        if (configBean.getUrl() != null) {
            properties.setProperty("url", configBean.getUrl());
        }
        if (configBean.getUserName() != null) {
            properties.setProperty("userName", configBean.getUserName());
        }
        if (configBean.getPassword() != null) {
            properties.setProperty("password", configBean.getPassword());
        }
        if (configBean.getFileName() != null) {
            properties.setProperty("fileName", configBean.getFileName());
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            properties.store(fileOutputStream, "can delete this file;  可删除;  " + LocalDateTime.now());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
